/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MathExpressionsDrawingPanel.java
 *
 * Created on 09-sep-2011, 20:59:17
 */

package br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels;

import Util.Scale;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;

/**
 *
 * @author frank
 */
public class MathExpressionsDrawingPanel extends DrawingPanel {
    protected boolean adaptExpressionToPanelSize=true;
    protected Scale scale;

    /** Creates new form MathExpressionsDrawingPanel */
    public MathExpressionsDrawingPanel() {
        initComponents();
        initializeAllFields();
    }

    protected void initializeAllFields(){
        super.initializeAllMyFields();
        adaptExpressionToPanelSize=true;
    }

    @Override
    protected void draw(Graphics g){
        if(drawable!=null){
            if(adaptExpressionToPanelSize)
                configurateScale(g);
            drawable.draw(g);
        }
    }

    public boolean isAdaptExpressionToPanelSize() {
        return adaptExpressionToPanelSize;
    }

    public void setAdaptExpressionToPanelSize(boolean adaptExpressionToPanelSize) {
        this.adaptExpressionToPanelSize = adaptExpressionToPanelSize;
    }

    public void configurateScale(Graphics g){
        scale=Scale.ScaleFromDimensions(drawable.getSize(),getSize());
        scale.scale();
        adaptExpressionToPanel(g);
    }

    private void adaptExpressionToPanel(Graphics g){
        Graphics2D g2D=(Graphics2D)g;
        if(scale.tooShort()||scale.tooThin()){
            g2D.scale(scale.getHorizontalScale(), scale.getVerticalScale());
        }
        Point2D position=scale.calculateScaledCentralPosition();
        drawable.translateTo(position);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}